package com.tqjc.system.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tqjc.system.common.constant.SQLConstant;
import com.tqjc.system.common.entity.BO.CarExecuteInfoBO;
import com.tqjc.system.common.entity.BO.TaskMaterialBO;
import com.tqjc.system.common.entity.VO.AddTaskVO;
import com.tqjc.system.common.entity.VO.CarExecuteInfoVO;
import com.tqjc.system.common.enums.TaskStatusEnum;
import com.tqjc.system.system.dao.SysAddDetailTaskDao;
import com.tqjc.system.system.entity.SysAddDetailTaskEntity;
import com.tqjc.system.system.service.SysAddDetailTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author 86195
 * @version 1.0
 * @description 加料任务详情服务类
 * @date 2023/10/16 15:08
 */
@Service("SysAddDetailTaskService")
@Slf4j
public class SysAddDetailTaskServiceImpl extends ServiceImpl<SysAddDetailTaskDao, SysAddDetailTaskEntity> implements SysAddDetailTaskService {
    @Override
    public List<String> getMaterialList(String taskId) {
        return baseMapper.getMaterialList(taskId);
    }

    @Override
    public List<TaskMaterialBO> sortSpanTaskMaterialList(String workingSpanNo, String taskId) {
        Map<String,String> paramMap = new HashMap<>(2);
        paramMap.put("workingSpanNo",workingSpanNo);
        paramMap.put("taskId",taskId);
        return baseMapper.sortSpanTaskMaterialList(paramMap);
    }

    @Override
    public SysAddDetailTaskEntity getMaterial(String taskId, String materialNo) {
        return  baseMapper.selectOne(new LambdaQueryWrapper<SysAddDetailTaskEntity>()
                .eq(SysAddDetailTaskEntity::getTaskId, taskId).eq(SysAddDetailTaskEntity::getMaterialNo, materialNo)
                .last(SQLConstant.SQL_LIMIT_1));

    }

    @Override
    public boolean modifyWeight(List<SysAddDetailTaskEntity> list,String msgBody) {
        try {
            int startIndex = 0;
            int num = Integer.valueOf(msgBody.substring(3, 5));
            for(int i = 0;i <= num;i++){
                startIndex = i * 76 + 5;
                String matCode = msgBody.substring( startIndex, 20 + startIndex);
                float matWt = Float.valueOf(msgBody.substring(startIndex + 70 ,startIndex + 76));
                Optional<SysAddDetailTaskEntity> optional = list.stream().filter(e -> e.getMaterialNo().equals(matCode)).findFirst();
                if(optional == null){
                    //本次新增的物料
                    String newMatName = msgBody.substring(20 + startIndex , 70 + startIndex).trim();
                    SysAddDetailTaskEntity detailTaskEntity = SysAddDetailTaskEntity.builder().targetWeight(matWt).materialNo(matCode).materialName(newMatName).build();
                    baseMapper.insert(detailTaskEntity);
                } else {
                    SysAddDetailTaskEntity detailTaskEntity = optional.get();
                    if(detailTaskEntity.getTargetWeight() != matWt){
                        SysAddDetailTaskEntity updateEntity = SysAddDetailTaskEntity.builder().idKey(detailTaskEntity.getIdKey())
                                .targetWeight(matWt).status(TaskStatusEnum.STATUS_UNDOING.getStatus()).build();
                        baseMapper.updateById(updateEntity);
                    }
                }
            }
        } catch (Exception e){
            log.error("任务更新操作，更新数据失败",e);
            return false;
        }

        return true;
    }

    @Override
    public List<SysAddDetailTaskEntity> getDetailTaskInfoByTankCarNo(String tankCarNo) {
        return baseMapper.getDetailTaskInfo(tankCarNo);
    }

    @Override
    public List<SysAddDetailTaskEntity> getDetailTaskInfo(String tankCarNo, String tempTaskId) {
        return baseMapper.selectList(new LambdaQueryWrapper<SysAddDetailTaskEntity>().eq(SysAddDetailTaskEntity::getTankCarNo,tankCarNo)
                .eq(SysAddDetailTaskEntity::getTaskId,tempTaskId));
    }

    @Override
    public void deleteMaterial(String idKey) {
        baseMapper.deleteById(idKey);
    }

    @Override
    public List<SysAddDetailTaskEntity> getDetailTaskInfo(String taskId) {
        return baseMapper.selectList(new LambdaQueryWrapper<SysAddDetailTaskEntity>()
                .eq(SysAddDetailTaskEntity::getTaskId,taskId));
    }

    @Override
    public List<AddTaskVO> getPrimaryTaskInfoByTankCarNo(String tankCarNo) {
        return baseMapper.getPrimaryTaskInfoByTankCarNo(tankCarNo);
    }

    @Override
    public boolean allTaskSuccess(String taskId) {
        int integer = baseMapper.selectCount(new LambdaQueryWrapper<SysAddDetailTaskEntity>().eq(SysAddDetailTaskEntity::getTaskId, taskId)
                .ne(SysAddDetailTaskEntity::getStatus, TaskStatusEnum.STATUS_COMPLETE.getStatus()));
        if(integer > 0){
            return false;
        }
        return true;
    }

    @Override
    public List<CarExecuteInfoBO> getCarExecuteInfo(LocalDateTime now,String status) {
        Map map = new HashMap(2);
        map.put("status",status);
        map.put("date",now.minusDays(2));
        return baseMapper.getCarExecuteInfo(map);
    }
}
